package com.ruoyi.web.controller.dj;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dj.domain.SpaceParamProduceParam;
import com.ruoyi.dj.domain.SpaceParamProduceVO;
import com.ruoyi.dj.service.ISpaceParamProduceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;


/**
 * <p>
 *  多模型计算与预报-动态修正计算
 * </p>
 */
@RestController
@RequestMapping("/dj/produce")
@Slf4j
@Api(tags = "多模型计算与预报-空间环境参数制备")
public class ParamProduceController {

    @Autowired
    private ISpaceParamProduceService produceService;


    @ApiOperation("制备")
    @PostMapping
    public R<List<SpaceParamProduceVO>> cal(@Validated @RequestBody SpaceParamProduceParam param) {
        List<SpaceParamProduceVO> vo = produceService.cal(param);
        return R.ok(vo);
    }

    @ApiOperation("结果保存")
    @PostMapping("/save")
    public R save(@Validated @RequestBody SpaceParamProduceParam param) {
        produceService.save(param);
        return R.ok();
    }

    @ApiOperation("结果下载")
    @PostMapping("/download")
    public void download(@Validated @RequestBody SpaceParamProduceParam param, HttpServletResponse response) {
        String file = produceService.download(param);
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, new File(file).getName());
            FileUtils.writeBytes(file, response.getOutputStream());
        }catch (Exception e) {
            log.error("下载失败",e);
        }
    }



}
